import csv
from django.db import transaction
from eskulap.profile.models import *


@transaction.commit_manually        
def load_gender ():
    reader = csv.reader (open('db/plec.csv'), delimiter = ';');
    try:
        for r in reader:
            g = Gender (name = r[1]);
            g.save ();
    except: 
        transaction.rollback ();
        return 'Plec - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'Plec - dane zostaly poprawnie zaimportowane';

@transaction.commit_manually        
def load_maritalstatus ():
    reader = csv.reader (open('db/stancywilny.csv'), delimiter = ';');
    try:
        for r in reader:
            s = MaritalStatus (name = r[1]);
            s.save ();
    except: 
        transaction.rollback ();
        return 'Stan cywilny - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'Stan cywilny - dane zostaly poprawnie zaimportowane';

@transaction.commit_manually        
def load_profile ():
    reader = csv.reader (open('db/pacjenci.csv'), delimiter = ';');
    try:
        for r in reader:
			#g =
			#s = 
            p = Profile (first_name = r[1], last_name = r[2], street = r[3], postal_code = r[4], city = r[5],
						birthday = '%s-%s-%s' % (r[7], r[8], r[9]), personal_id = r[10], gender = r[11], 
						marital_status = r[13], family_status = r[14], workplace = r[15], conditions = r[16],
						medical_history = r[17], family_illness = r[18]);
            p.save ();
    except: 
        transaction.rollback ();
        return 'Pacjenci - nastapil blad podczas importowania danych';
    else:
        transaction.commit ();
        return 'Pacjenci - dane zostaly poprawnie zaimportowane';
